USE [genshi]
GO

/****** Object:  Trigger [dbo].[Weapon_Down]    Script Date: 2022/5/3 20:28:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

Create TRIGGER [dbo].[Weapon_Down] ON [dbo].[Assemble]
After update
AS
Begin
	SET NOCOUNT ON;
	Declare @name nvarchar(10);
	Declare @weapon nvarchar(10);
	Declare @properties nvarchar(8);
	Declare @value real;
	select @name = inserted.Name from inserted
	/*确认是否卸掉了武器*/
	select @weapon = deleted.Weapon
	from deleted
	where deleted.Weapon is not null AND exists(select * from inserted where inserted.Name = deleted.Name AND inserted.Weapon is null);
	/*仅在武器被卸掉时执行*/
	if (@weapon is not null)
		begin
			update Assemble
			set Assemble.Attack = Assemble.Attack - (select Attack from Weapon where Weapon.Name = @weapon)
			where Assemble.Name = @name;

			select @properties = Weapon.Properties from Weapon where Weapon.Name = @weapon
			select @value = 0 - Weapon.Value from Weapon where Weapon.Name = @weapon

			if(@properties = '生命值')
				begin
					update Assemble
					set Assemble.LifePercentage = Assemble.LifePercentage + @value
					where Assemble.Name = @name;
				end
			else if(@properties = '攻击力')
				begin
					update Assemble
					set Assemble.AttackPercentage = Assemble.AttackPercentage + @value
					where Assemble.Name = @name;
				end
			else if(@properties = '防御力')
				begin
					update Assemble
					set Assemble.DefensePercentage = Assemble.DefensePercentage + @value
					where Assemble.Name = @name;
				end
			else if(@properties = '物理伤害加成')
				begin
					update Assemble
					set Assemble.Physics = Assemble.Physics + @value
					where Assemble.Name = @name;
				end
			else if(@properties = '元素充能效率')
				begin
					update Assemble
					set Assemble.Efficiency = Assemble.Efficiency + @value
					where Assemble.Name = @name;
				end
			else if(@properties = '元素精通')
				begin
					update Assemble
					set Assemble.Mystery = Assemble.Mystery + @value
					where Assemble.Name = @name;
				end
			else if(@properties = '暴击率')
				begin
					update Assemble
					set Assemble.Criticalhit = Assemble.Criticalhit + @value
					where Assemble.Name = @name;
				end
			else if(@properties = '暴击伤害')
				begin
					update Assemble
					set Assemble.Criticaldamage = Assemble.Criticaldamage + @value
					where Assemble.Name = @name;
				end
		end
End
GO

ALTER TABLE [dbo].[Assemble] ENABLE TRIGGER [Weapon_Down]
GO

